/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giant.bean;

import com.giant.authentication.Access;
import com.giant.services.authentication.ServiceAccessLocal;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

/**
 *
 * @author Harold
 */
@ManagedBean
@SessionScoped
public class DynamicMenu implements Serializable {

    //Atributos
//    private MenuModel simpleMenuModelAccount = new DefaultMenuModel();
//    private MenuModel simpleMenuModelOrder = new DefaultMenuModel();
//    private MenuModel simpleMenuModelWarranty = new DefaultMenuModel();
//    private MenuModel simpleMenuModelMarketing = new DefaultMenuModel();
    /**
     * Relación con la interfaz adecuada para coger los accesos de un profile
     */
    @EJB
    private ServiceAccessLocal serviceAccess;

    //Metodos
    public DynamicMenu() {
//        menuItem = new MenuItem();
//        menuItem.setValue("Trace Order");
//        menuItem.setUrl("#");
//        simpleMenuModelAccount.addMenuItem(menuItem);
//
//        menuItem = new MenuItem();
//        menuItem.setValue("Payment");
//        menuItem.setUrl("#");
//        simpleMenuModelAccount.addMenuItem(menuItem);
    }
    //Modules 1-order, 2-account, 3-warranty, 4-marketing

    public MenuModel getSimpleMenuModel(int modulo) {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        Map<String, Object> sessionMap = externalContext.getSessionMap();
        Integer prof = (Integer) sessionMap.get("profile");

        MenuModel menu = new DefaultMenuModel();
        MenuItem menuItem = new MenuItem();

        if (prof != null) {
            List<Access> accesss = serviceAccess.getAccesss("select a.* from access a join profile_access pa on a.id_access=pa.id_access join module m on a.id_module=m.id_module join profile p on pa.id_profile=p.id_profile where pa.id_profile=" + prof + " and a.id_module=" + modulo + " and m.status=1 and a.status=1 and p.status=1");

            switch (modulo) {
                case 1:
                    //Order
                    menuItem = new MenuItem();
                    menuItem.setValue("Order");
                    menuItem.setDisabled(true);
                    menuItem.setUrl("#");
                    menu.addMenuItem(menuItem);
                    break;
                case 2:
                    //Account
                    menuItem = new MenuItem();
                    menuItem.setValue("Account");
                    menuItem.setDisabled(true);
                    menuItem.setUrl("#");
                    menu.addMenuItem(menuItem);
                    break;
                case 3:
                    //Warranty
                    menuItem = new MenuItem();
                    menuItem.setValue("Warranty");
                    menuItem.setDisabled(true);
                    menuItem.setUrl("#");
                    menu.addMenuItem(menuItem);
                    break;
                case 4:
                    //Marketing
                    menuItem = new MenuItem();
                    menuItem.setValue("Marketing");
                    menuItem.setDisabled(true);
                    menuItem.setUrl("#");
                    menu.addMenuItem(menuItem);
                    break;
                case 5:
                    //POS
                    menuItem = new MenuItem();
                    menuItem.setValue("POS");
                    menuItem.setDisabled(true);
                    menuItem.setUrl("#");
                    menu.addMenuItem(menuItem);
                    break;

            }

            for (int i = 0; i < accesss.size(); i++) {
                menuItem = new MenuItem();
                menuItem.setValue(accesss.get(i).getLabel());
                menuItem.setUrl(accesss.get(i).getCodFunc());
                menu.addMenuItem(menuItem);
            }
        }



        return menu;
    }
}
